import {
  addTaskRecord,
  getAllTaskList as getAllTaskListUrl,
  getTodayTaskList,
  updateTaskRecord,
  getTaskDetail as getTaskDetailUrl
} from "../apis/index";
import { request } from "./http-base";

export interface TaskListItem {
  active: boolean;
  content: string;
  createDateTime: string;
  id: number;
  isRecord: true;
  lastUpdateTime: string;
  score: number;
  title: string;
  type: number;
}

// 所有任务列表
export const getAllTaskList = async (): Promise<TaskListItem[]> => {
  const res = await request(getAllTaskListUrl);
  return res?.data?.data || [];
};

export interface DailyTaskItem {
  createDateTime: string;
  finished: boolean;
  id: number;
  lastUpdateTime: string;
  recordDate: string;
  task: {
    active: boolean;
    content: string;
    createDateTime: string;
    id: number;
    lastUpdateTime: string;
    score: number;
    title: string;
    type: number;
  };
}

// 获取当日用户任务列表
export const getDailyTaskList = async (): Promise<DailyTaskItem[]> => {
  const res = await request(getTodayTaskList);
  return res?.data?.data || [];
};

// 将任务加入每日打卡
export const addDailyTaskList = async (taskId: number) => {
  return request(`${addTaskRecord}/${taskId}`, {}, "POST");
};

// 任务打卡
export const checkMarkTask = (taskRecordId: number) => {
  return request(updateTaskRecord, { taskRecordId: taskRecordId }, "PUT");
};

interface TaskDetail {
  active: boolean;
  content: string;
  createDateTime: string;
  id: number;
  lastUpdateTime: string;
  score: number;
  title: string;
  type: number;
}

// 获取任务详情
export const getTaskDetail = async (taskId: string): Promise<TaskDetail> => {
  const res = await request(`${getTaskDetailUrl}/${taskId}`);
  return res?.data?.data;
};
